package org.wang.generalcode.modules.exam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wang.generalcode.modules.exam.constants.EnumConstants;
import org.wang.generalcode.modules.exam.converter.QuestionInfoConverter;
import org.wang.generalcode.modules.exam.mapper.QuestionInfoMapper;
import org.wang.generalcode.modules.exam.parser.QuestionImportParser;
import org.wang.generalcode.modules.exam.parser.QuestionImportParserManager;
import org.wang.generalcode.modules.exam.pojo.entity.QuestionInfoEntity;
import org.wang.generalcode.modules.exam.pojo.param.QuestionInfoParams;
import org.wang.generalcode.modules.exam.pojo.vo.QuestionInfoVO;
import org.wang.generalcode.modules.exam.service.QuestionInfoService;
import org.wang.generalcode.support.base.BaseServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * 试题信息表服务实现类
 *
 * @author wz
 * @since 2024-11-22
 */
@Service
@RequiredArgsConstructor
public class QuestionInfoServiceImpl extends BaseServiceImpl<QuestionInfoMapper, QuestionInfoEntity> implements QuestionInfoService {

    private final QuestionInfoConverter questioninfoConverter;

    /**
     * 获取试题信息表分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<QuestionInfoVO>} 试题信息表分页列表
     */
    @Override
    public IPage<QuestionInfoVO> getQuestionInfoPage(IPage<QuestionInfoVO> page, QuestionInfoParams queryParams) {
        return page.setRecords(baseMapper.getQuestionInfoPage(page, queryParams));
    }

    @Override
    public int importQuestion(Integer schoolType, Integer gradeInfoId, Integer subjectId, List<QuestionInfoEntity> questionInfoList) {
        questionInfoList.forEach(questionInfo -> {
            for (EnumConstants.QuestionType item : EnumConstants.QuestionType.values()) {
                if (item.getName().equals(questionInfo.getQuestionTypeName())) {
                    questionInfo.setQuestionType(item.getValue());
                    break;
                }
            }
            QuestionImportParser excelQuestionParser = QuestionImportParserManager.build()
                    .createExcelQuestionParser(questionInfo.getQuestionType());
            questionInfo.setAnswer(excelQuestionParser.parseAnswerText(questionInfo.getAnswer()));
            questionInfo.setOptions(excelQuestionParser.parseOptionText(questionInfo.getOptions()));
            questionInfo.setGradeInfoId(gradeInfoId);
            questionInfo.setSchoolType(schoolType);
            questionInfo.setCreateTime(new Date());
            questionInfo.setSubjectId(subjectId);
        });
        super.saveBatch(questionInfoList);
        return questionInfoList.size();
    }
}
